/*
 * This file should not be edited.
 */
import database.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class TestingDataBase {

	public static void main(String[] args) {
		TestingDataBase testing = new TestingDataBase();

		testing.sampleCode();

	}

	public void sampleCode() {
		
		DataBase myDB, db;
		Table table, t;
		Record inRecord, outRecord;
		RecordID rid;
		List<RecordID> allRIDs = new ArrayList<RecordID>();
		//Iterator<RecordID> it = allRIDs.iterator();
		
		try {
			/*
			 * New Database Constructor:
			 * 
			 * @param, 1st parameter, name of the database
			 * 
			 * @param, 2nd parameter, size of the buffer pool in pages
			 */
			myDB = new DataBase("testDataBase", 5);

			/*
			 * New Table Schema Definition:
			 * 
			 * {{field type, field size in bytes, field name}, ...}
			 */
			Object[][] schema = { { "int", "4", "id" },
								{"int", "4", "salary" },
								{"varchar", "128", "name" },
								{"varchar", "128", "address" } };

			/*
			 * New Table Creation
			 * 
			 * @param, 1st parameter, name of the table/file
			 * 
			 * @param, 2nd parameter, schema
			 * 
			 * @return the table object as given by the class "Table" which should
			 * contain all relevant fields and methods
			 */
			table = myDB.createTable("Employee", schema);

			/*
			 * New Tuple Creation
			 */
			Object[] tuple = { new Integer("2"), new Integer("2000"),
				"Luke Skywalker", "Queens Gate 180" };

			/*
			 * Tuple Transformation Into Record for the Given Table
			 * 
			 * @param, 1st parameter, tuple object
			 * 
			 * @return, the record object as given by the class "Record" which
			 * should contain all relevant fields and methods
			 */
			inRecord = table.createRecord(tuple);

			/*
			 * Outputs all Record fields in human readable format
			 */
			System.out.println("New Record is: " +inRecord.toString());

			/*
			 * Tuple Insertion into Table
			 *
			 * @param, 1st parameter, record object 
			 * 
			 * @return the record identifier Object as given by the class "RecordID"
			 * which should contain all relevant fields and methods
			 */
			rid = table.insertRecord(inRecord);

			/*
			 * Outputs all RecordID fields in human readable format
			 */
			System.out.println("Record ID is: " +rid.toString());


			/*
			 * Reading the Record with rid Identifier
			 * 
			 * @param record identifier
			 * 
			 * @return the record object read
			 *
			 * @throws RecordIDNotFound
			 */
			outRecord = table.readRecord(rid);

			/*
			 * Compares all the fields of two records
			 * 
			 * @return boolean, true if they match, false otherwise
			 */
			if (outRecord.compareRecords(inRecord)) {
				System.out.println("Record: " +outRecord.toString()+ " and: "
						+inRecord.toString()+ " match.");
			} else {
				System.out.println("Record: " +outRecord.toString()+ " and: "
						+inRecord.toString()+ " do not match.");
			}
		

			/*
			 * Searches for the record with the given field value on the given field
			 * name
			 * 
			 * @param, 1st parameter, field name
			 * 
			 * @param, 2nd parameter, field value
			 * 
			 * @return List of record identifiers, if not found then List is empty
			 */
			allRIDs = table.searchRecord("id", new Integer(2));
			Iterator<RecordID> it = allRIDs.iterator();
			while (it.hasNext()) {
				rid = ((RecordID) it.next());

				/*
				 * Update the given field of the record with the "rid" identifier
				 * with a new value
				 * 
				 * @param, 1st parameter, record identifier
				 * 
				 * @param, 2nd parameter, field name
				 * 
				 * @param, 2rd parameter, field value
				 * 
				 * @throws RecordIDNotFound,
				 * NotEnoughSpaceInPage
				 */
				table.updateRecord(rid, "address", new String("Queens Gate 187"));
				
				Record out = table.readRecord(rid);

				/*
				 * Reads the field value of the given field from the given
				 * record
				 * 
				 * @param, 1st parameter, field name
				 * 
				 * @return the field value as an Object
				 */
				String updatedString = ((String)out.readFieldValue("address"));
				
				if (updatedString.compareTo("Queens Gate 187") == 0) {
					System.out.println("updateRecord was successful, new string is: "
									+updatedString);
				} else {
					System.out.println("updateRecord failed to write correct new fieldValue");
				}

				/*
				 * Delete a record with "rid" identifier
				 * 
				 * @param 1st parameter, rid identifier
				 * 
				 * @throws RecordIDNotFound
				 */
				table.deleteRecord(rid);
				System.out.println("Record with rid: "+rid.toString()+" was deleted.");
			}

			/*
			 * Closing the given table, flushing all table pages from memory to disk
			 */
			table.close();

			/*
			 * Closing the database, flushing all database pages from memory to disk
			 */
			myDB.close();

			/*
			 * the default DataBase constructor
			 */
			db = new DataBase();

			/*
			 * opens an existing database 
			 */
			db.openDataBase("testDataBase", 5);

			/*
			 * opens an existing database table
			 */
			t = db.openTable("Employee");

			t.close();
			db.close();
			
		} catch (Exception e) {
			System.out.println("Exception: "+e);
		}
	}
}

